﻿
Partial Class pages_products_ProductTranscript : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property ProductId() As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            Product = CProduct.Cache.GetById(ProductId)
            If IsNothing(Product) Then CSitemap.RecordNotFound("Product", ProductId)
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductTranscription(Me.Product.ProductId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With Product
            MenuSelected = "Products"
            UnbindSideMenu()
            AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
            AddMenuSide(Product.TypeName & " Details", CSitemap.ProductEdit(ProductId))
            AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId))
            If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
            If .CanHaveTranscript Then
                AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId), True)
            End If
            AddMenuSide("Vimeo Link" & IIf(.HasVideosOnVimeo, "*", String.Empty) & IIf(.HasVideosOnYouTube, "*", String.Empty), CSitemap.ProductVimeo(ProductId))
            If .CanHaveFiles Then
                AddMenuSide("Dropbox Files", CSitemap.ProductFiles(ProductId))
            Else
                AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", CFile.Cache.GetByProductId(Me.ProductId)), CSitemap.ProductFiles(ProductId))
            End If
            If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId))
                AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId))
            End If
            If .HasParent Then AddMenuSide("Back to Set...", CSitemap.ProductContents(.ProductBelongsToProductId))


            ddScribe.DataSource = CScribe.Cache.ForText
            ddScribe.DataBind()
            CDropdown.BlankItem(ddScribe, "-- None --", 0)
            CDropdown.SetValue(ddScribe, .ProductScribeId)

            txtTranscription.Text = .ProductTranscription
            If .IsEnglish Then
                txtTransChinSimp.Visible = False
                txtTransChinTrad.Visible = False
            Else
                txtTransChinSimp.Text = .ProductTranscriptionChineseSimplified
                txtTransChinTrad.Text = .ProductTranscriptionChineseTraditional
            End If
        End With
    End Sub
    Protected Overrides Sub PagePreRender()
        With Product
            txtTranscription.Visible = .ProductScribeId > 0
            btnEdit.Visible = .ProductScribeId > 0
            btnCancel.Visible = .ProductScribeId > 0
            btnSave.Visible = .ProductScribeId > 0
            btnDelete.Visible = .ProductScribeId > 0
            lnkScribe.NavigateUrl = CSitemap.ScribeEdit(.ProductScribeId)
            lnkScribe.Visible = .HasTranscript
            If .HasTranscript Then lnkScribe.Text = .ScribeName
        End With
    End Sub
#End Region

    Protected Sub ddScribe_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddScribe.SelectedIndexChanged
        Product.ProductScribeId = CDropdown.GetInt(ddScribe)
        Product.Save()

        If Product.ProductScribeId = 0 Then
            Refresh()
        Else
            btnEdit_Click(sender, e)
        End If
    End Sub

    Protected Sub btnEdit_Click(sender As Object, e As System.EventArgs) Handles btnEdit.Click
        txtTranscription.Mode = EControlMode.Editable
        txtTransChinSimp.Mode = EControlMode.Editable
        txtTransChinTrad.Mode = EControlMode.Editable

        btnEdit.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        btnDelete.Enabled = True
    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click
        Refresh()
    End Sub

    Private Const cr1 As String = vbCrLf
    Private Const cr2 As String = vbCrLf & vbCrLf
    Private Const cr4 As String = vbCrLf & vbCrLf & vbCrLf & vbCrLf
    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        With Product
            .ProductTranscription = txtTranscription.Text.Replace(cr1, cr2).Replace(cr4, cr2)
            If Not .IsEnglish Then
                .ProductTranscriptionChineseSimplified = txtTransChinSimp.Text.Replace(cr1, cr2).Replace(cr4, cr2)
                .ProductTranscriptionChineseTraditional = txtTransChinTrad.Text.Replace(cr1, cr2).Replace(cr4, cr2)
            End If
            .Save()
        End With
        Refresh()
    End Sub

    Protected Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles btnDelete.Click
        Product.ProductScribeId = 0
        Product.Save()
        Refresh()
    End Sub

End Class
